package org.example.xsda.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import org.example.xsda.common.PageResult;
import org.example.xsda.common.Result;
import org.example.xsda.entity.StudentScore;
import org.example.xsda.service.ScoreService;
import org.example.xsda.vo.StudentScoreVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 成绩控制器
 */
@RestController
@RequestMapping("/score")
public class ScoreController {

    @Autowired
    private ScoreService scoreService;

    @GetMapping("/page")
    @SaCheckRole(value = {"admin", "teacher"}, mode = SaMode.OR)
    public Result<PageResult<StudentScoreVO>> page(
            @RequestParam(defaultValue = "1") Long current,
            @RequestParam(defaultValue = "10") Long size,
            @RequestParam(required = false) Long studentId,
            @RequestParam(required = false) String semester,
            @RequestParam(required = false) String courseName,
            @RequestParam(required = false) String studentKeyword
    ) {
        PageResult<StudentScoreVO> pageResult = scoreService.page(current, size, studentId, semester, courseName, studentKeyword);
        return Result.success(pageResult);
    }

    @GetMapping("/student/{studentId}")
    public Result<List<StudentScore>> getByStudentId(@PathVariable Long studentId) {
        List<StudentScore> list = scoreService.getByStudentId(studentId);
        return Result.success(list);
    }

    @SaCheckRole(value = {"admin", "teacher"}, mode = SaMode.OR)
    @PostMapping
    public Result<Void> add(@Validated @RequestBody StudentScore score) {
        scoreService.add(score);
        return Result.success();
    }

    @SaCheckRole(value = {"admin", "teacher"}, mode = SaMode.OR)
    @PutMapping
    public Result<Void> update(@Validated @RequestBody StudentScore score) {
        scoreService.update(score);
        return Result.success();
    }

    @SaCheckRole(value = {"admin", "teacher"}, mode = SaMode.OR)
    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        scoreService.delete(id);
        return Result.success();
    }
    
    /**
     * 学生查询自己的成绩
     */
    @SaCheckRole("student")
    @GetMapping("/my-scores")
    public Result<PageResult<StudentScore>> getMyScores(
            @RequestParam(defaultValue = "1") Long current,
            @RequestParam(defaultValue = "10") Long size,
            @RequestParam(required = false) String semester,
            @RequestParam(required = false) String courseName
    ) {
        PageResult<StudentScore> pageResult = scoreService.getMyScores(current, size, semester, courseName);
        return Result.success(pageResult);
    }
    
    /**
     * 学生导出自己的成绩单
     */
    @SaCheckRole("student")
    @GetMapping("/export-my-scores")
    public void exportMyScores(HttpServletResponse response) {
        scoreService.exportMyScores(response);
    }
}

